//
//  ZJCourseNameView.m
//  ZhongWangYuanApp
//
//  Created by ZJ on 2018/6/2.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "ZJCourseNameView.h"

@interface ZJCourseNameView ()
/** 标题 */
@property (nonatomic, strong) UILabel *nameLabel;
/** 查看   图片*/
@property (nonatomic, strong) UIImageView *lookImage;
/** 查看   文字 */
@property (nonatomic, strong) UILabel *lookLabel;
/** 评分  图片 */
@property (nonatomic, strong) UIImageView *numberImage;
/** 评分  文字*/
@property (nonatomic, strong) UILabel *numberLabel;
/** 书 图片 */
@property (nonatomic, strong) UIImageView *bookImage;
/** 书 文字 */
@property (nonatomic, strong) UILabel *bookLabel;
/** 听课的切换按钮 */
@property (nonatomic, strong) UIButton *switchButton;
/** 下划线 */
@property (nonatomic, strong) UILabel *underLine;
@end
@implementation ZJCourseNameView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initUI];
    }
    return self;
}

- (void)initUI{
    self.lookImage = [self creatImageWithImageName:@"details_view"];
    self.numberImage = [self creatImageWithImageName:@"details_c"];
    self.bookImage = [self creatImageWithImageName:@"details_rt"];
    self.lookLabel = [self creatLabel];
    self.numberLabel = [self creatLabel];
    self.bookLabel = [self creatLabel];
    [self addSubview:self.nameLabel];
    [self addSubview:self.lookImage];
    [self addSubview:self.numberImage];
    [self addSubview:self.bookImage];
    [self addSubview:self.lookLabel];
    [self addSubview:self.numberLabel];
    [self addSubview:self.bookLabel];
    [self addSubview:self.switchButton];
    [self addSubview:self.underLine];
    
    
    
    self.switchButton.sd_layout
    .rightSpaceToView(self, 12 * [UIScreen zj_screenWScale])
    .heightIs(35 * [UIScreen zj_screenHScale])
    .widthIs(80 * [UIScreen zj_screenWScale])
    .centerYEqualToView(self);
    
    self.switchButton.sd_cornerRadius = @(self.switchButton.height / 2);
    
    
    self.nameLabel.sd_layout
    .topSpaceToView(self, 12 * [UIScreen zj_screenHScale])
    .leftSpaceToView(self, 12 * [UIScreen zj_screenWScale])
    .rightSpaceToView(self.switchButton, 10 * [UIScreen zj_screenWScale])
    .heightIs([NSString heightWithTextFont:self.nameLabel.font]);
    
    self.lookImage.sd_layout
    .leftEqualToView(self.nameLabel)
    .topSpaceToView(self.nameLabel, 10 * [UIScreen zj_screenHScale])
    .widthIs(15 * [UIScreen zj_screenWScale])
    .heightEqualToWidth();
    
    self.lookLabel.sd_layout
    .topEqualToView(self.lookImage)
    .leftSpaceToView(self.lookImage, 3 * [UIScreen zj_screenWScale])
    .heightRatioToView(self.lookImage, 1)
    .widthIs(10);
    
    self.numberImage.sd_layout
    .topEqualToView(self.lookImage)
    .leftSpaceToView(self.lookLabel, 12 * [UIScreen zj_screenWScale])
    .widthRatioToView(self.lookImage, 1)
    .heightEqualToWidth();
    
    self.numberLabel.sd_layout
    .topEqualToView(self.lookImage)
    .leftSpaceToView(self.numberImage, 3 * [UIScreen zj_screenWScale])
    .heightRatioToView(self.lookImage, 1)
    .widthIs(10);
    
    self.bookImage.sd_layout
    .topEqualToView(self.lookImage)
    .leftSpaceToView(self.numberLabel, 12 * [UIScreen zj_screenWScale])
    .widthRatioToView(self.lookImage, 1)
    .heightEqualToWidth();
    
    self.bookLabel.sd_layout
    .topEqualToView(self.lookImage)
    .leftSpaceToView(self.bookImage, 3 * [UIScreen zj_screenWScale])
    .heightRatioToView(self.lookImage, 1)
    .widthIs(10);
    
    self.underLine.sd_layout
    .topSpaceToView(self.bookImage, 12 * [UIScreen zj_screenHScale])
    .leftEqualToView(self)
    .rightEqualToView(self)
    .heightIs(1);
    
    [self setupAutoHeightWithBottomView:self.underLine bottomMargin:0];
}


#pragma mark - 点击事件
- (void)action_switchButtonClick:(UIButton *)sender{
    sender.selected = !sender.selected;
    BOOL isSwithVideo = NO;
    if (sender.selected) {//选中状态 显示 看课，正在播放的是音频
        isSwithVideo = NO;
    }else{
        isSwithVideo = YES;
    }
    if (self.delegate) {
        [self.delegate ZJCourseNameViewDelegate_isSwithVideo:isSwithVideo];
    }
}

///对label赋值
- (void)label:(UILabel *)label updateWidthFromString:(NSString *)string{
    label.text = string;
    label.sd_layout.widthIs([NSString zj_sizeOfString:string font:label.font width:self.width].width);
    [label updateLayout];
}


#pragma mark - Setter
- (void)setModel:(LPZWYChooseClassClassModel *)model{
    _model = model;
    self.nameLabel.text = model.name ? model.name :model.coursewarename;
    [self label:self.lookLabel updateWidthFromString:[NSString string:model.clickrate isNilStringReturnDeftString:@"0"]];
    [self label:self.numberLabel updateWidthFromString:[NSString stringWithFormat:@"%.2f",[model.grade floatValue]]];
    [self label:self.bookLabel updateWidthFromString:[NSString string:model.studytime isNilStringReturnDeftString:@"0"]];
    
    //根据audio_url、teachervideo 来判断
    if ((model.audio_url&& model.audio_url.length>0) && (model.teachervideo && model.teachervideo.length > 0)) {
        self.switchButton.hidden = NO;
        self.nameLabel.sd_layout
        .topSpaceToView(self, 12 * [UIScreen zj_screenHScale])
        .leftSpaceToView(self, 12 * [UIScreen zj_screenWScale])
        .rightSpaceToView(self.switchButton, 10 * [UIScreen zj_screenWScale])
        .heightIs([NSString heightWithTextFont:self.nameLabel.font]);
    }else{
        self.switchButton.hidden = YES;
        self.nameLabel.sd_layout
        .topSpaceToView(self, 12 * [UIScreen zj_screenHScale])
        .leftSpaceToView(self, 12 * [UIScreen zj_screenWScale])
        .rightSpaceToView(self, 12 * [UIScreen zj_screenWScale])
        .heightIs([NSString heightWithTextFont:self.nameLabel.font]);
        }
    [self.nameLabel updateLayout];
    
}

- (void)setVideoPlayType_isVideo:(BOOL)isVideo{
    self.switchButton.selected = !isVideo;
}

#pragma mark - Getters
- (UIImageView *)creatImageWithImageName:(NSString *)imageName{
    UIImageView * imageView = [UIImageView new];
    imageView.image = [UIImage imageNamed:imageName];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    return imageView;
}

- (UILabel *)creatLabel{
    UILabel * label = [UILabel new];
    label.font = LYPFONT(14);
    label.textColor = [UIColor textGrayColor];
    return label;
}


- (UILabel *)nameLabel{
    if (_nameLabel == nil){
        _nameLabel  = [UILabel new];
        _nameLabel.font = LYPFONT(16);
        _nameLabel.textColor = [UIColor textGrayColor];
    }
    return _nameLabel;
}


- (UIButton *)switchButton{
    if (_switchButton == nil){
        _switchButton = [UIButton new];
        [_switchButton setTitle:@" 听课" forState:UIControlStateNormal];
        [_switchButton setTitle:@" 看课" forState:UIControlStateSelected];
        [_switchButton setImage:[UIImage imageNamed:@"details_l"] forState:UIControlStateNormal];
        [_switchButton setImage:[UIImage imageNamed:@"details_lc"] forState:UIControlStateSelected];
        [_switchButton setTitleColor:[UIColor themeColor] forState:UIControlStateNormal];
        _switchButton.titleLabel.font = LYPFONT(16);
        _switchButton.layer.borderColor = [UIColor themeColor].CGColor;
        _switchButton.layer.borderWidth = 1;
        [_switchButton addTarget:self action:@selector(action_switchButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _switchButton;
}

- (UILabel *)underLine{
    if (_underLine == nil){
        _underLine = [UILabel new];
        _underLine.backgroundColor = [UIColor themeGrayColor];
    }
    return _underLine;
}

@end
